﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StuCat
{
    public partial class Minipanel : Panel
    {
        public String Caption { get { return label1.Text; } set { label1.Text = value; } }
        public bool Opened { get; private set; }

        Timer openTimer = new Timer();
        Timer closeTimer = new Timer();

        readonly int closedSize;
        const int scrollStep = 7;
        const int timerStep = 5;

        bool scrolling = false;
        private int oldHeight;


        public void Open()
        {
            if (Opened||scrolling) return;            

            scrolling = true;
            openTimer.Start();
            Opened = true;
        }
        
        
        void Closing_Scroll(object sender, EventArgs e)
        {
            this.Height -=  Math.Min(scrollStep, this.Height - closedSize) ;
            
            
            if (this.Height == closedSize)
            {                
                closeTimer.Stop();                
                scrolling = false;

                label1.ActiveLinkColor = label1.LinkColor;                
            }

            // Computing label color 
            int newHeight = closedSize;
            int colorByte = Math.Min(255, (int)(255 * ((float)newHeight / this.Height)));
            label1.LinkColor = Color.FromArgb(colorByte, 0, 255 - colorByte);
        }


        void Opening_Scroll(object sender, EventArgs e)
        {
            this.Height +=  Math.Min(scrollStep, oldHeight - this.Height) ;
           
            if (this.Height == oldHeight)
            {                                
                openTimer.Stop();
                scrolling = false;

                label1.ActiveLinkColor = label1.LinkColor;                         
            }

            // Computing label color 
            int colorByte = Math.Min(255, (int)(255 * ((float)this.Height / oldHeight)));
            label1.LinkColor = Color.FromArgb(255 - colorByte, 0, colorByte);                                           
        }
        
        
        public void Close()
        {
            if (!Opened||scrolling) return;

            oldHeight = this.Height;
            this.AutoSize = false;
            
            this.Height = oldHeight;
            
            scrolling = true;
            closeTimer.Start();

            Opened = false;
        }        

      
        public Minipanel()
        {
            InitializeComponent();
         
            Opened = true;
            this.BorderStyle = BorderStyle.FixedSingle;

            openTimer.Interval = timerStep;
            openTimer.Tick += new EventHandler(Opening_Scroll);
            
            closeTimer.Interval = timerStep;
            closeTimer.Tick += new EventHandler(Closing_Scroll);


            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            closedSize = label1.Height + 2;
        }

      
        private void label1_Click(object sender, EventArgs e)
        {         
            if (Opened)
                Close();
            else
                Open();
        }

        private void label1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }      
    }
}

